import { MongoClient } from "mongodb";
import chalk from "chalk";
import { parseTitle, processContent } from "./utils.js";
import { MONGO_URI, MONGO_DB, SOURCE_COLLECTION } from "./config.js";

async function updateData() {
  const client = new MongoClient(MONGO_URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  });

  try {
    // 连接到 MongoDB
    await client.connect();
    console.log(chalk.bold.yellow("MongoDB connection connected"));
    const db = client.db(MONGO_DB);
    const sourceCollection = db.collection(SOURCE_COLLECTION);

    // 创建一个游标来遍历集合
    // title:{'$regex': '.*方案.*篇.*'}  content:{'$regex': '.*附件'}
    // 文章不规范：存在"附件:" 、"附件："两种情况
    // {
      // {
      //   spider_time: { $regex: "2025-01-(06|07|08|09|10)"},
      // },
      // { platform_id: "ft_diyifanwen" , "$expr": { "$lt": [ { "$strLenCP": "$content" }, 300 ] } }
    
    const cursor = sourceCollection.find(
      { platform_id: "ft_diyifanwen" ,  },
      { noCursorTimeout: true }
    );
    
    let content_list = []
    for await (const doc of cursor) {
      // 添加 type 字段
      const content = processContent(doc.content);
      const content_list = content.split('\n')
      const title_0 = content_list[0]
      console.log('标题：',title_0);
      
      const updatedDoc = {
        ...doc,
        title:title_0,
        // spider_time:doc.spider_time.substring(0,19), // 前提日期有问题
        // content,
      };

      // 更新到 SOURCE_COLLECTION
      await sourceCollection.updateOne({ _id: doc._id }, { $set: updatedDoc });
      console.log(chalk.green(`Updated document: ${doc._id}`));
    }

    console.log(chalk.bold.yellow("Success: all docs updated"));
  } catch (err) {
    console.error(err);
  } finally {
    console.log(chalk.bold.yellow("MongoDB connection closed"));
    await client.close();
  }
}

// 执行数据更新
updateData().catch(console.error);
